﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace WPFTest
{
    /// <summary>
    /// MainWindow.xaml 的交互逻辑
    /// </summary>
    public partial class MainWindow : Window,INotifyPropertyChanged
    {
        public MainWindow()
        {
            InitializeComponent();
            DataContext = this;
        }



        ObservableCollection<TestDirectoryInfo> _DirList = new ObservableCollection<TestDirectoryInfo>();
        public ObservableCollection<TestDirectoryInfo> DirList { get { return _DirList; } }

        TestDirectoryInfo _SelectedDirectory = null;
        public TestDirectoryInfo SelectedDirectory { get => _SelectedDirectory;
            set {
                _SelectedDirectory = value;
                RaisePropertyChanged(nameof(SelectedDirectory));
            }
        }

        public FileSystemInfo SelectedFile { get; set; }



        private void OpenDirMenu_Click(object sender, RoutedEventArgs e)
        {
            OpenDirBTN_Click(sender, e);
        }

        private void OpenDirBTN_Click(object sender, RoutedEventArgs e)
        {
            FolderBrowserDialog dlg = new FolderBrowserDialog();
            if (dlg.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                Title = "MainWindow - " + dlg.SelectedPath;
                DirList.Add( new TestDirectoryInfo( dlg.SelectedPath));
            }
        }

        private void Tree_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            var dir = e.NewValue as TestDirectoryInfo;
            if (dir == null) return;
            SelectedDirectory = dir;
        }

        private void FileListView_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            if (SelectedFile == null) return;

            var f = SelectedFile as FileInfo;
            if (f != null)
            {
                Process.Start(f.FullName);
            }
        }

        #region PropertyChanged
        public event PropertyChangedEventHandler PropertyChanged;
        public void RaisePropertyChanged(string propName)
        {
            PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propName));
        }


        #endregion

        private void CloseMenu_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void CloseBTN_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }
    }
}
